class Component < ActiveRecord::Base
  attr_accessible :composite, :name, :component_type, :description

  def self.components_collect(composite_soa)
    comp_name =composite_soa.name
    composite_soa.components.collect do |cmt|
      cmt_name =cmt.attribute('name').value
      component = Component.where(name: cmt_name, composite: comp_name).first
      if component.nil?
	component = Component.new
	component.name = cmt_name
	component.composite = comp_name
	fill_fields(component)
      end
      component.component_type = composite_soa.implementation_type(cmt)
      component.save if component.new_record? or component.changed?
      component
    end
  end
  
  def self.fill_fields(component)
    if component.description.nil? or component.description.length == 0
      cmt = Component.find(:all, :conditions => "name = '#{component.name}' and description <> ''").first
      component.description = cmt.description if !cmt.nil?
    end
  end
  
end
